package com.framework.dubbo.producer.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.framework.dubbo.producer.config.Constants;
import com.service.http.service.HttpService;
        import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * 算法接口调用
 * @author zhaoj
 * @create 2022/2/22
 */
@Component
public class AlgorithmUtil {

    @Autowired
    private HttpService httpService;

    @Value("${akgorithm.base-url}")
    public  String  ALGORITHM_BASE_URL;

    /**
     * ocr识别
     * @param is
     * @param fileName
     * @return
     */
    public String ocr(InputStream is, String fileName){
        try {
            String result = httpService.upload(ALGORITHM_BASE_URL + "/ocr", is, fileName);
            JSONObject dataObj = JSON.parseObject(result);
            if(dataObj.getIntValue("code") == 200) {
                return dataObj.getString("words");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Constants.EMPTY;
    }

}
